package org.example;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}
class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode cur = dummyHead, prev,next, tail;
        while(cur.next != null && cur.next.next != null){
            prev = cur.next;
            next = prev.next;
            tail = next.next;
            //两两交换
            cur.next = next;
            next.next = prev;
            prev.next = tail;

            cur = cur.next.next;
        }

        return dummyHead.next;
    }
}